<template>
	<view class='page_cont'>
		<view class='choose_img' @click="choose_image()">
			<image class='avatar' :src="avatar"></image>
			<view class='text_wrap'>
				<view class='title'>编辑头像</view>
				<view class='text'>上传真实的头像，求职成功率更高哦</view>
			</view>
		</view>
		<view class='check_item'>
			<view class='title'>姓名</view>
			<input type="text" class='input' :value="nickName" maxlength='15' placeholder="请输入姓名" placeholder-style="color:rgba(121,133,148,0.5);"
			 @input="input_name">
			<!-- <image class='icon' src="/static/icon7.png"></image> -->
		</view>
		<view class='check_item'>
			<view class='title'>性别</view>
			<view class='kong'></view>
			<view class='check_wrap'>
				<view class='item' @click="check_sex(1)">
					<image class='icon' :src="sex==1?'/static/icon46.png':'/static/icon45.png'"></image>
					<view class='text'>男</view>
				</view>
				<view class='item' @click="check_sex(2)">
					<image class='icon' :src="sex==2?'/static/icon46.png':'/static/icon45.png'"></image>
					<view class='text'>女</view>
				</view>
			</view>
		</view>
		<view class='check_item' @click="check_pop(1)">
			<view class='title'>出生日期</view>
			<view class='nat'>{{birthday}}</view>
			<image class='icon' src="/static/icon7.png"></image>
		</view>
		<view class='check_item' @click="check_pop(2)">
			<view class='title'>现居地区</view>
			<view class='nat'>{{region}}</view>
			<image class='icon' src="/static/icon7.png"></image>
		</view>
		<view class='check_item' @click="check_pop(3)">
			<view class='title'>毕业时间</view>
			<view class='nat'>{{finish}}</view>
			<image class='icon' src="/static/icon7.png"></image>
		</view>
		<view class='check_item' @click="check_pop(5)">
			<view class='title'>学历情况</view>
			<view class='nat'>{{education}}</view>
			<image class='icon' src="/static/icon7.png"></image>
		</view>
		<view class='check_item no_border' @click="check_pop(4)">
			<view class='title'>教师资格证</view>
			<view class='nat'>{{cret}}</view>
			<image class='icon' src="/static/icon7.png"></image>
		</view>
		<view class='com_btn' @click="confirm_dom()">{{submitBtn}}</view>
		<pickerPop :showMask="show_mask" :showIndex="show_index" :title="title" :pickers="pickers" :value="value" @closeMask="close_mask"
		 @pickerChange="picker_change" @confirmValue="confirm_value" :address="address" :time='time'></pickerPop>
	</view>
</template>

<script>
	import PickerPop from "../../components/pickerPop.vue"
	import other from "../../utils/other.js"
	import basic from '../../api/config.js'
	import resume from '../../api/myResume.js'
	import util from "../../utils/util.js"
	export default {
		data() {
			return {
				img: '',
				id: '',
				avatar: "http://ossdev0.jiabala.com/bduHjrswEDdFO",
				nickName: '',
				sex: '',
				show_mask: false,
				show_index: '',
				title: '',
				check_index: '',
				pickers: [],
				address: false,
				time: false,
				area_value: [0, 0, 0],
				value: [0],
				times: [],
				birthday: '',
				birthday_value: [0, 0, 0],
				regions: [
					["广东", "广东"],
					["广州", "广州"],
					["天河", "天河"]
				],
				region: '',
				region_value: [0, 0, 0],
				liveCode: [],
				finish: '',
				finish_value: [0, 0, 0],
				educations:[],
				education:'',
				educationId:'',
				education_value:[0],
				crets: [{
					name: "有教师资格证",
					id: '1'
				}, {
					name: "教师资格证考取中",
					id: '2'
				}, {
					name: "无教师资格证",
					id: '3'
				}],
				cret: '',
				cret_value: [0],
				liveAddressProvince: '',
				liveAddressCity: '',
				liveAddressRegion: '',
				liveAddressCode: '',
				userBasicInfo: {},
				checkId:'',
				liveAddressProvince:'',
				liveAddressCity:'',
				liveAddressRegion:'',
				liveAddressCode:'',
				editId:'',
				switchOpen:true,
				submitBtn:'保存'
			}
		},
		onLoad(options) {
			this.checkId=options.id
			var that = this;
			const value = uni.getStorageSync('userinfo');
			this.id = value.id
			that.times = other.dateTimePicker(1910, 2010).dateTimeArray;
			this.getEducationList()
			if (+options.id > 0) {
				this.submitBtn='修改'
				resume.check_user_info({
					id: options.id
				}).then(res => {
					this.userBasicInfo = res.data.data
					const { data } = res.data
					console.log(data);
					this.editId=data.id
					this.avatar = data.avatar
					this.nickName = data.nickName
					this.sex = data.sex
					this.birthday = this.format(data.birthday)
					this.finish = this.format(data.joinWorkTime)
					this.education=data.educationName
					this.cret = this.crets[data.certificateStatus-1].name
					this.region = data.liveAddressProvince + '-' + data.liveAddressCity + '-' + data.liveAddressRegion
					this.liveAddressProvince=data.liveAddressProvince
					this.liveAddressCity=data.liveAddressCity
					this.liveAddressRegion=data.liveAddressRegion
					this.liveAddressCode=data.liveAddressCode
					
				})
			}
		},
		methods: {
			randomString(length, chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
				var result = ''
				for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)]
				return result
			},
			install(file, isAccept = false) {
				return new Promise((resolve, reject) => {
					let imgUrl = ''
					basic.get_oss().then(response => {
						const info = response.data.data
						const randomStr = this.randomString(13)
						const headers = {
							'Content-Type': 'multipart/form-data',
							'Access-Control-Allow-Origin': '*'
						}
						const param = {
							key: randomStr,
							policy: info.policy,
							OSSAccessKeyId: info.accessKeyId,
							success_action_status: 200,
							signature: info.signature,
						}
						uni.showLoading({
							title: '上传中...'
						});
						uni.uploadFile({
							url: info.host,
							filePath: file.tempFilePath,
							name: 'file',
							formData: param,
							success: (uploadFileRes) => {
								uni.hideLoading();
								const url = info.host + '/' + randomStr
								resolve(url)
							},
							fail: (err) => {
								console.log(err);
							}
						});
					})
				})
			},
			// 选择照片
			choose_image() {
				uni.chooseImage({
					count: 1,
					sourceType: ['album', 'camera'],
					success: (res) => {
						this.avatar = res.tempFilePaths[0]
						const config = {
							name: res.path || '',
							tempFile: res.tempFiles[0],
							tempFilePath: res.tempFilePaths[0],
							size: res.tempFiles[0].size,
							type: 1,
						}
						this.install(config).then(response => {
							this.avatar = response
							console.log(response);
						})
					}
				})
			},
			// 输入框
			input_name(e) {
				this.nickName = e.detail.value;
			},
			// 选择性别
			check_sex(index) {
				this.sex = index;
			},
			// 选择框
			check_pop(index) {
				this.check_index = index;
				switch (index) {
					case 1:
						this.show_index = 3;
						this.title = '出生时间';
						this.time = true;
						this.value = this.birthday_value;
						this.show_mask = true;
						break;
					case 2:
						this.show_index = 3;
						this.title = '现居地区';
						this.address = true;
						this.value = this.region_value;
						this.show_mask = true;
						break;
					case 3:
						this.show_index = 3;
						this.title = '毕业时间';
						this.time = true;
						this.value = this.finish_value;
						this.show_mask = true;
						break;
					case 4:
						this.show_index = 1;
						this.title = '教师资格证';
						this.pickers = this.crets;
						this.value = this.cret_value;
						this.show_mask = true;
						break;
					case 5:
						this.show_index = 1;
						this.title = '学历情况';
						this.pickers = this.educations;
						this.value = this.education_value;
						this.show_mask = true;
						break;
				}
			},
			// 关闭选择框
			close_mask() {
				this.show_mask = false
				this.time = false
				this.address = false
			},
			// 选择
			picker_change(res) {
				console.log(res);
				var val = res,
					index = this.check_index;
				this.value = val;
			},
			// 确认
			confirm_value(data) {
				var index = this.check_index;
				switch (index) {
					case 1:
						this.birthday_value = this.value;
						this.birthday = data[0] + '-' + data[1] + '-' + data[2]
						break;
					case 2:
						this.region_value = this.value;
						this.region = data[0]['shortName'] + '-' + data[1]['shortName'] + '-' + data[2]['shortName'];
						this.liveAddressProvince = data[0]['shortName']
						this.liveAddressCity = data[1]['shortName']
						this.liveAddressRegion = data[2]['shortName']
						this.liveAddressCode = data[0]['provinceCode'] + ',' + data[1]['cityCode'] + ',' + data[2]['areaCode']
						break;
					case 3:
						this.finish_value = this.value;
						this.finish = data[0] + '-' + data[1] + '-' + data[2]
						break;
					case 4:
						this.cret_value = this.value;
						this.cret = this.crets[this.value[0]].name
						break;
					case 5:
						this.education_value = this.value;
						this.education = this.educations[this.value[0]].name
						break;
				}
				this.close_mask();
			},
			// 保存信息
			confirm_dom() {
				let valiteObj=[
					{
						value:this.nickName,
						show_toast:'姓名不能为空'
					},
					{
						value:this.sex+'',
						show_toast:'性别不能为空'
					},
					{
						value:this.birthday,
						show_toast:'出生日期不能为空'
					},
					{
						value:this.finish,
						show_toast:'毕业时间不能为空'
					}
				]
				for(const key of valiteObj){
					if(key.value===''){
						util.page_show_toast(key.show_toast, 2000)
						return
					}
				}
				this.educations.forEach(item=>{
					if(this.education===item.name){
						this.educationId=item.id
					}
				})
				if (this.checkId) {
					let aboutTeacher = ''
					this.crets.forEach(item => {
						if (item.name === this.cret) {
							aboutTeacher = item.id
						}
					})

					let config = {
						avatar: this.avatar,
						userId: this.id,
						nickName: this.nickName,
						sex: this.sex,
						birthday: new Date(this.birthday).getTime(),
						liveAddressProvince: this.liveAddressProvince,
						liveAddressCity: this.liveAddressCity,
						liveAddressRegion: this.liveAddressRegion,
						liveAddressCode: this.liveAddressCode,
						joinWorkTime: new Date(this.finish).getTime(),
						certificateStatus: aboutTeacher,
						id:this.editId,
						educationId:this.educationId,
						educationName:this.education,
						isPublic:1
					}
					console.log(config.avatar);
					if(this.switchOpen){
						resume.edit_user_info(config).then(res => {
							if(res.data.code==='200'){
								util.page_show_toast('修改成功', 2000)
								this.switchOpen=false
								setTimeout(() => {
									// uni.switchTab({
									// 	url:'../../pages/mypage/mypage'
									// })
									var pages = getCurrentPages();
									var prevPage = pages[pages.length - 2];
									prevPage.$vm.screening=true
									uni.navigateBack({
										delta: 1
									})
								}, 2000)
								this.save_login_status(res.data.data.id,1)
							}
						})
					}
				} else {
					let aboutTeacher = ''
					this.crets.forEach(item => {
						if (item.name === this.cret) {
							aboutTeacher = item.id
						}
					})
					let config = {
						avatar: this.avatar,
						userId: this.id,
						nickName: this.nickName,
						sex: this.sex,
						birthday: new Date(this.birthday).getTime(),
						liveAddressProvince: this.liveAddressProvince,
						liveAddressCity: this.liveAddressCity,
						liveAddressRegion: this.liveAddressRegion,
						liveAddressCode: this.liveAddressCode,
						joinWorkTime: new Date(this.finish).getTime(),
						certificateStatus: aboutTeacher,
						isLogin:1,
						educationId:this.educationId,
						educationName:this.education,
						isPublic:1
					}
					if(this.switchOpen){
						resume.add_user_info(config).then(res => {
							if (res.data.code === '200') {
								uni.setStorageSync('identityNum', 1);
								this.switchOpen=false
								uni.switchTab({
									url: '../../pages/index/index'
								})
								this.save_login_status(res.data.data.id,2)
							}
						})
					}
				}
			},
			// 时间处理
			add0(m) {
				return m < 10 ? '0' + m : m
			},
			format(shijianchuo) {
				var time = new Date(shijianchuo);
				var y = time.getFullYear();
				var m = time.getMonth() + 1;
				var d = time.getDate();
				return y + '-' + this.add0(m) + '-' + this.add0(d);
			},
			// 保存登录信息
			save_login_status(id,identity){
				resume.save_login_identity({id:id,identity:identity}).then(res=>{
					console.log(res);
				})
			},
			// 获取学历情况
			getEducationList(){
				resume.get_education_list().then(res=>{
					this.educations=res.data.data
				})
			}
		},
		components: {
			pickerPop: PickerPop
		}
	}
</script>

<style lang="scss">
	.page_cont {
		padding: 16rpx 32rpx 0 32rpx;

		.choose_img {
			padding-bottom: 40rpx;
			border-bottom: 2rpx solid #F7F9FB;
			display: flex;
			flex-direction: row;

			.avatar {
				width: 116rpx;
				height: 116rpx;
				border-radius: 50%;
				margin-right: 16rpx;
			}

			.text_wrap {
				flex: 1;
				padding-top: 12rpx;

				.title {
					text-align: right;
					height: 50rpx;
					font-size: 36rpx;
					font-weight: 600;
					line-height: 50rpx;
					color: #3F4A58;
					margin-bottom: 6rpx;
				}

				.text {
					text-align: right;
					height: 34rpx;
					font-size: 24rpx;
					font-weight: 400;
					line-height: 34rpx;
					color: rgba(121, 133, 148, 0.5);
				}
			}
		}

		.check_item {
			padding: 32rpx 0;
			border-bottom: 2rpx solid #F7F9FB;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;

			.title {
				height: 42rpx;
				font-size: 30rpx;
				font-weight: 600;
				line-height: 42rpx;
				color: #3F4A58;
			}

			.input {
				/* #ifdef MP-WEIXIN */
				justify-content: flex-end;
				/* #endif */
				/* #ifdef H5 */
				width: 80%!important;
				/* #endif */
				/* #ifdef APP-PLUS */
				width: 80%!important;
				/* #endif */
				// flex: 1;
				text-align: right;
				height: 40rpx;
				font-size: 28rpx;
				font-weight: 400;
				line-height: 40rpx;
				color: #798594;
				margin: 0 24rpx;
				display: flex;
			}

			.icon {
				width: 24rpx;
				height: 24rpx;
			}

			.kong {
				flex: 1;
				margin: 0 24rpx;
			}

			.check_wrap {
				display: flex;
				flex-direction: row;

				.item {
					display: flex;
					flex-direction: row;
					align-items: center;

					&:first-child {
						margin-right: 48rpx;
					}

					.icon {
						width: 32rpx;
						height: 32rpx;
						margin-right: 20rpx;
					}

					.text {
						height: 40rpx;
						font-size: 28rpx;
						font-weight: 400;
						line-height: 40rpx;
						color: #798594;
					}
				}
			}

			.nat {
				flex: 1;
				text-align: right;
				height: 40rpx;
				font-size: 28rpx;
				font-weight: 400;
				line-height: 40rpx;
				color: #798594;
				margin: 0 24rpx;
			}
		}

		.no_border {
			border-bottom: 0;
		}

		.com_btn {
			position: fixed;
			bottom: 32rpx;
			left: 0;
			right: 0;
			width: 686rpx;
			margin: auto;
			height: 104rpx;
			text-align: center;
			line-height: 104rpx;
			background: #5B90FF;
			border-radius: 16rpx;
			color: #fff;
			font-size: 36rpx;
			font-weight: 400;
		}
	}
</style>
